//
//  SetManagerVC.swift
//  YueChat
//
//  Created by 1 on 2024/11/1.
//

import UIKit

enum SetGroupMemberType {
    case Manager
    case Mute
}

class SetManagerVC: BaseVC {

    var type = SetGroupMemberType.Manager
    var group = DMCCGroup()

    
    init(group:DMCCGroup,type:SetGroupMemberType) {
        super.init(nibName: nil, bundle: nil)
        self.type = type
        self.group = group
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        switch type {
        case .Manager:
            self.title = "设置管理员"
            break
        case .Mute:
            self.title = "设置禁言"
            break
        default: break
            
        }
        tableView.register(UINib(nibName: "UserCell", bundle: nil), forCellReuseIdentifier: "UserCell")
        getData()
        
        navigationItem.rightBarButtonItem = rt_textItemWithTarget(self, action: #selector(done), title: "完成")
    }
    
    @objc func done () {
        
        var array = [String]()
        dataSource.forEach { member in
            let m = member as! DMCCMember
            if m.isSelect == true {
                array.append(m.memberId)
            }
        }
        
        if array.count == 0 {
            MHAlert.showMessage("请选择成员")
            return
        }
        MHAlert.showLoading()
        if type == .Manager {
            Task {
                let res = await  gosnGroup.addGroupManager(group.groupId, array)
                await MainActor.run {
                    if res  {
                        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: DispatchWorkItem(block: {
                            MHAlert.showMessage("设置成功")
                            self.navigationController?.popViewController(animated: true)
                        }))
                    }else{
                        MHAlert.showMessage("设置失败")
                    }
                }
            }
        }
        
        if type == .Mute {
            Task {
                let res = await  gosnGroup.muteMembers(group.groupId, array, true)
                await MainActor.run {
                    if res  {
                        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: DispatchWorkItem(block: {
                            MHAlert.showMessage("设置成功")
                            self.navigationController?.popViewController(animated: true)
                        }))
                    }else{
                        MHAlert.showMessage("设置失败")
                    }
                }
            }
        }
        
       
    }
    
    
    override func getData () {
        let list = gosnMember.getMemberList(group.groupId, 0, group.memberCount)
        
        
        switch type {
        case .Manager:
            list.forEach { member in
                if member.type == .Normal {
                    dataSource.append(member)
                }
            }
            break
        case .Mute:
            list.forEach { member in
                if member.mute == 0 {
                    dataSource.append(member)
                }
            }
            break
        default:
            break
        }
        
        tableView.reloadData()
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserCell
        cell.selectImg.isHidden = false
        let member = dataSource[indexPath.row] as! DMCCMember
        let user = gosnUser.getUserInfoDefault(member.memberId)
        cell.setUserInfo(userInfo: user)
        if member.isSelect {
            cell.selectImg.image = UIImage(named: "select_s")
        }else{
            cell.selectImg.image = UIImage(named: "select_n")
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
        let member = dataSource[indexPath.row] as! DMCCMember
        
        member.isSelect = !member.isSelect
        tableView.reloadData()
//        if selects.contains(where: { $0.memberId == member.memberId }) {
//            
//        } else {
//            print("对象数组不包含具有ID为2的对象")
//        }
            
            

    }
    
    deinit {
        
    }
}
